package com.shk.controller;

        import com.alibaba.dubbo.config.annotation.Reference;
        import com.shk.constant.MessageConstant;
        import com.shk.entity.Result;
        import com.shk.pojo.Setmeal;
        import com.shk.service.MemberService;
        import com.shk.service.OrderService;
        import com.shk.service.ReportService;
        import com.shk.utils.DateUtils;
        import org.apache.poi.ss.usermodel.Workbook;
        import org.apache.poi.xssf.usermodel.XSSFCell;
        import org.apache.poi.xssf.usermodel.XSSFRow;
        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;

        import javax.servlet.ServletOutputStream;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.io.File;
        import java.io.FileInputStream;
        import java.text.DateFormat;
        import java.text.SimpleDateFormat;
        import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;
    @Reference
    private ReportService reportService;

    /*前端需要数据类型*/
  /*  months: ['2020-08', '2020-08', '2020-08', '2020-08', '2020-08', '2020-08', '2020-08'  日期]
    memberCountt: [820, 932, 901, 934, 1290, 1330, 1320],  人数*/
  /*折线图*/
    @RequestMapping("/getMemberReport")
    public Result linkChart(){
       /*获取时间一年之内的12个月*/
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List list = new ArrayList<>();
        for(int i=0;i<12;i++){
            /*每次遍历往前推一个月*/
            calendar.add(Calendar.MONTH,1);
            list.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
            }
     Map map = new HashMap();
        map.put("months",list);

        List byOneYear=null;
        try {
            byOneYear = memberService.findByOneYear(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
        map.put("memberCount",byOneYear);
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);
    }


    /*饼状图*/
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        List<Map> nameAndCount = reportService.findNameAndCount();
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,nameAndCount);
    }

    /*运营数据统计*/

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map data = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        try {
            Map result = reportService.getBusinessReportData();

            String templatePath = request.getSession().getServletContext().getRealPath("template")+ File.separator+"report_template.xlsx";
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(templatePath)));

            XSSFSheet sheet = excel.getSheetAt(0);
            sheet.getRow(2).getCell(5).setCellValue((String) result.get("reportDate"));
            sheet.getRow(4).getCell(5).setCellValue((Integer) result.get("todayNewMember"));
            sheet.getRow(4).getCell(7).setCellValue((Integer) result.get("totalMember"));
            sheet.getRow(5).getCell(5).setCellValue((Integer) result.get("thisWeekNewMember"));
            sheet.getRow(5).getCell(7).setCellValue((Integer) result.get("thisMonthNewMember"));
            sheet.getRow(7).getCell(5).setCellValue((Integer) result.get("todayOrderNumber"));
            sheet.getRow(7).getCell(7).setCellValue((Integer) result.get("todayVisitsNumber"));
            sheet.getRow(8).getCell(5).setCellValue((Integer) result.get("thisWeekOrderNumber"));
            sheet.getRow(8).getCell(7).setCellValue((Integer) result.get("thisWeekVisitsNumber"));
            sheet.getRow(9).getCell(5).setCellValue((Integer) result.get("thisMonthOrderNumber"));
            sheet.getRow(9).getCell(7).setCellValue((Integer) result.get("thisMonthVisitsNumber"));
            List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
            int rowNull = 11;
            for(Map setmeal:hotSetmeal){
               /* setmeal.get("name");
                setmeal.get("setmeal_count");
                setmeal.get("proportion");*/
                XSSFRow row = sheet.getRow(rowNull++);
                row.getCell(4).setCellValue((String) setmeal.get("name"));
                row.getCell(5).setCellValue((Long) setmeal.get("setmeal_count"));
                row.getCell(6).setCellValue((String) setmeal.get("proportion"));
            }

            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }
    /*public static void main(String[] args) throws Exception {
        Calendar calendar = Calendar.getInstance();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));

        calendar.add(Calendar.MONTH,-12);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        System.out.println("1111" +
                "1111");
      List list = new ArrayList<>();
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,1);

            list.add(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        }
        System.out.println(list);

    }*/
/*
    public static void main(String[] args) {
        String date = "2020-08-31";

    }*/
}
